from Cell import *
from Move import *

class Board:
    """
    The Board class contains the various things that are game specific.
    
    A board knows how to print itself. How to perform a move on the
    board itself, and should know whether or not a move is legal.
    """
    
    def __init__(self,boardSeed="empty"):
        self.board = []
        for i in range(0,9):
            self.board.append([])
            for j in range(0,9):
                self.board[i].append(Cell())
        self.allow_illegal = False
        if boardSeed != "empty":
            self.generateRandomBoard(boardSeed)

    def doMove(self,move):
        (x,y) = move.getLoc()
        val = move.getVal()
        response = self.mark(x,y,val)
        return response

    def mark(self,x,y,val):
        valid = self.checkValidity(x,y,val)
        legal = self.checkLegality(x,y,val)
        if not valid:
            return (False,"This is not a valid move (out of bounds)")
        elif self.board[x][y].locked:
            return (False,"This spot on the board is locked")
        elif self.allow_illegal or legal:
            self.board[x][y].value = val
            return (True,"Successfully marked the board")
        else:
            return (False,"This is an illegal move")
        
    def toString(self):
        printBoard = " ------------------ \n"
        for i in range(0,9):
            printBoard += "|"
            for each in self.board[i]:
                printBoard += each.toString() + " "
            printBoard += "|\n"
        printBoard += " ------------------ \n"
        return printBoard
    
    def checkValidity(self,x,y,val):
        if val < 1 or val > 9:
            return False
        if x < 0 or x > 8:
            return False
        if y < 0 or y > 8:
            return False
        return True
    
    def checkLegality(self,x,y,val):
        for i in range(0,9):
            if (val == self.board[i][y].value) or (val == self.board[x][i].value):
                return False
        
        (xb,yb) = self.index2BoxIndex(x,y)
        
        for i in range(0,3):
            for j in range(0,3):
                if (val == self.board[i+3*xb][j+3*yb].value):
                    return False
                
        return True

    def index2BoxIndex(self,x,y):
        xBox = 0
        yBox = 0
        if (0 <= x) and (x <= 2):
            xBox = 0
        elif (3 <= x) and (x <= 6):
            xBox = 1
        elif (7 <= x) and (x <= 2):
            xBox = 2
        else:
            xBox = -1
            
        if (0 <= y) and (y <= 2):
            yBox = 0
        elif (3 <= y) and (y <= 6):
            yBox = 1
        elif (7 <= y) and (y <= 2):
            yBox = 2
        else:
            yBox = -1
        
        return (xBox,yBox)
    
    def generateRandomBoard(self, boardSeed):
        import random
        
        for i in range(0,9):
            for j in range(0,9):
                success = 0
                for k in range(1,10):
                    success = self.mark(i,j,k)
                    if success[0]:
                        break
                #print self.toString()
        
                